/**
 * 
 */
package com.regedit.aplicacion.adapters;

import android.content.Context;
import android.database.Cursor;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckedTextView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

import com.regedit.aplicacion.dao.EntityDao;
import com.regedit.aplicacion.models.Carro;

/**
 * @author pjohnson
 * 
 */
public class MiSpinnerAdapter extends SimpleCursorAdapter {

	public MiSpinnerAdapter(Context context, int layout, Cursor c,
			String[] from, int[] to) {
		super(context, layout, c, from, to);
	}

	/**
	 * Get the View of the new item to be displayed in the screen
	 */
	@Override
	public View getView(final int position, View convertView,
			final ViewGroup parent) {

		final TextView viewRelative = (TextView) super.getView(position,
				convertView, parent);
		final Cursor cursor = getCursor();
		cursor.moveToPosition(position);

		viewRelative.setText(cursor.getString(cursor
				.getColumnIndexOrThrow(EntityDao.COL_TITLE)));

		return viewRelative;
	}

	@Override
	public Object getItem(int position) {
		final Carro carro = new Carro();
		final Cursor cursor = getCursor();
		carro.setId(cursor.getLong(cursor
				.getColumnIndexOrThrow(EntityDao.COL_ID)));
		carro.setMarca(cursor.getString(cursor
				.getColumnIndexOrThrow(EntityDao.COL_TITLE)));

		return carro;
	}

	@Override
	public View getDropDownView(int position, View convertView, ViewGroup parent) {
		final CheckedTextView viewRelative = (CheckedTextView) super
				.getDropDownView(position, convertView, parent);
		final Cursor cursor = getCursor();
		cursor.moveToPosition(position);

		viewRelative.setText(cursor.getString(cursor
				.getColumnIndexOrThrow(EntityDao.COL_TITLE)));

		return viewRelative;
	}

}
